<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006-2016 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 流年 <liu21st@gmail.com>
// +----------------------------------------------------------------------

// 应用公共文件
function httpGET($target_url, $get_data = array()){
        $result = file_get_contents($target_url.'?'.http_build_query($get_data));
        $result=json_decode($result);
        return $result;
}


/**
 * 是否为空
 * @param unknown $r
 * @return boolean
 */
function if_null($r){
        if($r===null || $r==='' || $r===false ){
                return true;
        }else{
                return false;
        }
}

function cur_time(){
        return date('Y-m-d H:i:s');
}

function object2array(&$object) {
        $object =  json_decode( json_encode( $object),true);
        return  $object;
}

function pic_host($img){
        //var_dump($_SERVER);
    if($img==false){
            $pic=get_http_host()."/404.png";
            return $pic;
    }elseif(!strstr($img,"http://")){
            $pic=get_http_host().$img;
            return $pic;
    }else{
            return $img;
    }
}

function get_http_host(){
        return "http://".$_SERVER["HTTP_HOST"];
}

/**
 *
 *
 *
 * @param array $arr 一维数组
 * @param array $v_arr
 * [
 *   ["key"=>'name','null_del'=>"姓名不能为空"],
 *   ["key"=>'mobile','null_del'=>"姓名不能为空"],
 * ]
 *
 *
 *
 */
function arr_null_deal($arr=array(),$v_arr=[]){
        //dump($v_arr);
        if($arr==false){
                return ['code'=>1,'tip'=>$v_arr[0]['null_del']];
        }

        foreach($v_arr as $k1=>$v1){
                $r=tt2($v1['key'],$arr);
                if($r==false){
                        return ['code'=>1,'tip'=>$v1['null_del']];
                }

        }
        return ['code'=>0,'tip'=>'ok'];
}


/**
 * 返回指定键值的内容，是否有内容
 * @param $key
 * @param array $arr
 * @return bool
 */
function tt2($key,$arr=array()){


        foreach($arr as $k=>$v){
                if($k==$key && $v!=false){
                        return true;
                }
        }
        return false;
}

function upload_pic($input_name,$input_value=''){
        include("./template/back/upload_html.html");
}

/**
 * 根据地址转坐标，通过腾讯地图
 * @param $address
 * @return array
 */
function get_x_y_by_address($address){
        $key="ESNBZ-ZLRCQ-67X5A-GYWCB-YLQLK-JHBRS";
        $url="https://apis.map.qq.com/ws/geocoder/v1/?address={$address}&key={$key}";
        $r=file_get_contents($url);
        $res=json_decode($r,true);
        if($res['status']==0){
                return ['code'=>0,'res'=>$res['result']['location'],'tip'=>'地图返回坐标成功'];
        }else{
                return ['code'=>1,'res'=>[],'tip'=>'地图返回坐标失败'];
        }
}


/*
 * $x = diffDate('2018-12-29','2019-01-29');
 */
function diffDate($date1,$date2){
        if(strtotime($date1)>strtotime($date2)){
                $tmp=$date2;
                $date2=$date1;
                $date1=$tmp;
        }
        list($Y1,$m1,$d1)=explode('-',$date1);
        list($Y2,$m2,$d2)=explode('-',$date2);
        $Y=$Y2-$Y1;
        $m=$m2-$m1;
        $d=$d2-$d1;
        if($d<0){
                $d+=(int)date('t',strtotime("-1 month $date2"));
                $m--;
        }
        if($m<0){
                $m+=12;
                $Y--;
        }
        return array('year'=>$Y,'month'=>$m,'day'=>$d);
}

function select_html($name,$value,$arrs){
       $str="";
        $str.="<option  value=''>--请选择--</option>";
       foreach($arrs as $k=>$v){
               if($value==$k){
                       $str.="<option selected='selected' value={$k}>{$v}</option>";
               }else{
                       $str.="<option value={$k}>{$v}</option>";
               }

       }
        $str="<select name='{$name}'>".$str."</select>";
       return $str;
}

function if_yes_no(){
        $arr=[0=>'否',1=>'是'];
        return $arr;
}

function get_key_arr_by_arrs($arrs,$key,$name){
        foreach($arrs as $k=>$v){
                $tem_arr[$v[$key]]=$v[$name];
        }
        return $tem_arr;
}
